package com.link.play.fit.ai.ui.dialog

import android.app.Activity
import android.app.AlertDialog
import android.util.Log
import android.view.LayoutInflater
import com.aigestudio.wheelpicker.WheelPicker.OnWheelChangeListener
import com.link.play.fit.ai.R
import com.link.play.fit.ai.databinding.DialogSetTimeBinding

class SetTimeDialog(
    val activity: Activity
) {

    private val TAG = "SetTimeDialog"
    private val dialog = AlertDialog.Builder(activity).create()
    private val binding = DialogSetTimeBinding.inflate(LayoutInflater.from(activity))
    private val hourList by lazy {
        val list = mutableListOf<String>()
        var start = 1 - 1
        repeat(12 - start) {
            start++
            if (start < 10) {
                list.add("0$start")
            } else {
                list.add("$start")
            }
        }
        list.toList()
    }

    private val minList by lazy {
        val list = mutableListOf<String>()
        var start = 0 - 1
        repeat(59 - start) {
            start++
            if (start < 10) {
                list.add("0$start")
            } else {
                list.add("$start")
            }
        }
        list.toList()
    }
    private val apmList by lazy {
        listOf("AM", "PM")
    }

    fun showDialog(time: String, action: (fTime: String) -> Unit) {

        dialog.setView(binding.root)
        dialog.setCanceledOnTouchOutside(false)
        dialog.setCancelable(false)
        dialog.show()

        dialog?.window?.setBackgroundDrawableResource(android.R.color.transparent)
        val params = dialog?.window?.attributes
        params?.width = activity.resources.getDimensionPixelOffset(R.dimen.dp_350)
        dialog?.window?.attributes = params


        binding.wheelHour.data = hourList
        binding.wheelMin.data = minList
        binding.wheelAPM.data = apmList

        var hour = "12"
        var min = "22"
        var apm = "AM"

        if (time.isNotEmpty()) {
            val split = time.split(":")
            hour = split[0]
            min = split[1]
            apm = split[2]
        }

        binding.wheelHour.setSelectedItemPosition(hourList.indexOf(hour), false)
        binding.wheelMin.setSelectedItemPosition(minList.indexOf(min), false)
        binding.wheelAPM.setSelectedItemPosition(apmList.indexOf(apm), false)

        binding.wheelHour.setOnWheelChangeListener(object : OnWheelChangeListener {
            override fun onWheelScrolled(offset: Int) = Unit
            override fun onWheelScrollStateChanged(state: Int) = Unit
            override fun onWheelSelected(position: Int) {
                hour = hourList[position]
            }
        })
        binding.wheelMin.setOnWheelChangeListener(object : OnWheelChangeListener {
            override fun onWheelScrolled(offset: Int) = Unit
            override fun onWheelScrollStateChanged(state: Int) = Unit
            override fun onWheelSelected(position: Int) {
                min = minList[position]
            }
        })
        binding.wheelAPM.setOnWheelChangeListener(object : OnWheelChangeListener {
            override fun onWheelScrolled(offset: Int) = Unit
            override fun onWheelScrollStateChanged(state: Int) = Unit
            override fun onWheelSelected(position: Int) {
                apm = apmList[position]
            }
        })

        binding.tvNo.setOnClickListener {
            dialog.dismiss()
        }
        binding.tvYes.setOnClickListener {
            dialog.dismiss()
            val fTime = "$hour:$min:$apm"
//            Log.e(TAG, "fTime=$fTime")
            action.invoke(fTime)
        }

    }

}